<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>${SYSTEM_NAME}</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/layui/css/layui.css?v=${FILE_VERSION}" media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/layuiadmin/style/admin.css?v=${FILE_VERSION}" media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath}/static/js/zTree_v3/css/metroStyle/metroStyle.css?v=${FILE_VERSION}" type="text/css">
</head>
<body>
  	<div class="layui-fluid">
  		<div class="layui-card">
  			<div class="layui-card-header">机构用户管理</div>
  			<div class="layui-card-body">
  				<div class="layui-row layui-col-space10">
		  			<div class="layui-col-xs2">
		    			<div style="border: 1px solid #ccc;max-height: 550px;min-height:400px; overflow: auto;" >
							<ul id="treeRegion" class="ztree"></ul>
						</div>
		  			</div>
  
			  	  	<div class="layui-col-xs10" style="padding-left: 15px">
				  		<div class="layui-form-item">
				  			<div class="layui-inline">
				      			<div class="layui-btn-group">
					   				<button class="layui-btn " onclick="addLayer()">新增管理员</button>
					   				<button class="layui-btn layui-btn-danger" onclick="deleteBatch()">删除</button>
					   			</div>
				    		</div>
				  		</div>
				  		<table class="layui-table" lay-filter="tableList" id="layerTable">
				  		</table>
				  	</div>
			  	</div>
			</div>
	  	</div>
  	</div>
  
  	<script type="text/html" id="dateTpl">
		{{  new Date(d.dateCreate).format("yyyy-MM-dd hh:mm:ss") }}
	</script>
	
  	<script type="text/html" id="barDemo">
  		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>
	
	<script src="${pageContext.request.contextPath}/static/js/common-2.0.0.js?v=${FILE_VERSION}"></script>
	<script src="${pageContext.request.contextPath}/static/layuiadmin/layui/layui.js?v=${FILE_VERSION}"></script>
	<script src="${pageContext.request.contextPath}/static/js/jquery-3.3.1.min.js?v=${FILE_VERSION}"></script>
	
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.core.js?v=${FILE_VERSION}"></script>
			
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.excheck.js?v=${FILE_VERSION}"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/zTree_v3/js/jquery.ztree.exedit.js?v=${FILE_VERSION}"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/common/zTree.drop.js?v=${FILE_VERSION}"></script>
	<script>
	//Ztree生成
	var parentName = "";
	var ZtreeObj;
	
	var updateTableCell;
	var setting = {
		data: {
			simpleData: {
				enable: false
			}
		},
		edit: {
			enable: false
		},
		async: {
			enable: true,
			url: getUrl
		},
		callback : {
			onClick: treeOnClick 
		}
	};
	
	function treeOnClick(event, treeId, treeNode) {
		parentName = treeNode.name;
		reload(treeNode.id);
	}
	
	function getUrl(treeId, treeNode) {
		return  getContextPath() + '/sys/org/list/getByParentId?parentId=' + treeNode.id;
	}
	
	var table;
	layui.config({
		base: getContextPath() + '/static/layuiadmin/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['index', 'table', 'element'], function() {
		var element = layui.element;
		layer = layui.layer;
	    table = layui.table;
	    table.render({ //其它参数在此省略
		  elem : '#layerTable',
		  even :"true",
		  limit: 10,
		  limits: [10,20,30],
		  id: 'tableList',
		  url: getContextPath() + '/sys/orgUser/list/listInfo',
		  page:true, 
		  method: 'post', //如果无需自定义HTTP类型，可不加该参数
		  cols:  [[ //标题栏
	         {checkbox: true, fixed: true}
	         ,{field:'orgName', minWidth:120,title:"机构名称"}
	         ,{field:'userRealName', width:200,title:"用户名称"}
	         ,{field:'isManageName', width:200,title:"是否管理员"}
	         ,{fixed: 'right', width:260, align:'center',title:"操作", toolbar: '#barDemo'}
	       ]],
		  request: {
              pageName: 'current' //页码的参数名称，默认：page
              ,limitName: 'size' //每页数据量的参数名，默认：limit
		  }
		});
	  
	  //监听工具条
	  table.on('tool(tableList)', function(obj){
	    var data = obj.data;
	    if(obj.event === 'del'){
	    	deleteById(data,obj);
	    } else if(obj.event === 'edit'){
	    	addLayer(data.id,data.parentId);
	    	updateTableCell = obj;
	    }
	  });
	});
	
	function reload(pId){
		if(typeof(pId) != "undefined")
			$('#parentId').val(pId);
	  	var parentId = $('#parentId').val();
	  	table.reload('tableList', {
            page: {curr: 1}, //重新从第 1 页开始
		 	where: {
			  "parentId": parentId,
			  "name" : $('#name').val()
	       }
	  	});
	}
	
	function addLayer(id, editParentName){
		var parentId = $('#parentId').val();
		var url,title;
		if(id == null){
			url = getContextPath() +  '/sys/orgUser/add/input?parentId=' + parentId + "&parentName=" + parentName;
			title = "添加机构信息";
		}
		else{
			url = getContextPath() +  '/sys/orgUser/edit/input?id=' + id + "&parentId=" + parentId + "&parentName=" + editParentName;
			title = "修改" + editParentName + "机构信息";
		}
		layer.open({
		  title : title,
		  type: 2,
		  scrollbar :true,
		  area: ['90%', '90%'],
		  anim: 2,
		  content: [url, 'yes']
		});
	}
	
    function selectUser(id, name){
		var url = getContextPath() +  '/sys/org/selectUser?id=' + id;
        layer.open({
            title : name + "管理员设置",
            type: 2,
            scrollbar :true,
            area: ['90%', '90%'],
            anim: 2,
            content: [url, 'yes']
        });
    }

	function deleteById(data,obj){
		layer.confirm('真的删除此项记录么', function(index){
			$ajax({
				url: "/sys/orgUser/del/deleteById",
				data: {id : data.id},
				succCallback: function (json) {
					if(json.code == 0){
						obj.del();
						var deleteNode = ZtreeObj.getNodeByParam("id", data.id, null);
						ZtreeObj.removeNode(deleteNode);
					}
				}
			});
		});
	}

	function deleteBatch(){
		var checkStatus = table.checkStatus('tableList')
				,data = checkStatus.data;
		var ids = "";
		if(data.length == 0 ){
			layer.msg("请至少选择一条数据");
			return false;
		}else{
			for (var i = 0; i < data.length; i++) {
				ids += data[i].id;
				if(i != (data.length -1))
					ids += ",";
			}
		}
		layer.confirm('您确定要批量删除数据吗？此操作不可逆转，请谨慎使用！', function(index){
			$ajax({
				url: "/sys/orgUser/del/deleteByIds",
				data: {"ids" : ids},
				succCallback: function (json) {
					if(json.code == 0)
						reload();
				}
			});
		});
	}
	
	$(document).ready(function(){
		$.ajax({
		    url : getContextPath() + "/sys/org/list/getByParentId",
		    type : "post",
		    dataType : "text",
		    success : function(data) {
		    	var zNodes = eval("(" + data + ")");
		    	ZtreeObj = $.fn.zTree.init($("#treeRegion"), setting, zNodes);
		    },
		    error : function(XMLHttpRequest, textStatus) {
		    	layer.msg(textStatus);
		    }
		});
	});
	</script>
</body>
</html>